package week10;

//*******************************************************************
//  MagazineList.java       Author: Lewis/Loftus
//
//  Represents a collection of magazines.
//*******************************************************************

public class MagazineList
{
    private MagazineNode list;

    //----------------------------------------------------------------
    //  Sets up an initially empty list of magazines.
    //----------------------------------------------------------------
    public MagazineList()
    {
        list = null;
    }

    //----------------------------------------------------------------
    //  Creates a new MagazineNode object and adds it to the end of
    //  the linked list.
    //----------------------------------------------------------------
    public void add(Magazine mag)
    {
        MagazineNode node = new MagazineNode(mag);
        MagazineNode current;

        if (list == null)
            list = node;
        else
        {
            current = list;
            while (current.next != null)
                current = current.next;
            current.next = node;
        }
    }

    public void insert(int index, Magazine mag) {
        MagazineNode node = list;
        int i = 0;
        while (node != null && i < index - 2) {
            node = node.next;
            i++;
        }
        MagazineNode sert = new MagazineNode(mag);
        sert.next = node.next;
        node.next = sert;

        if (index == 1){//此处为补丁，由我和王文彬同学交流合作得之
            Magazine temp = list.magazine;
            list.magazine = list.next.magazine;
            list.next.magazine =temp;
        }
    }

    public void delete(Magazine mag){
        MagazineNode node = list;
        while(node.next.magazine != mag){
            node = node.next;
        }
        node.next = node.next.next;
    }



    //----------------------------------------------------------------
    //  Returns this list of magazines as a string.
    //----------------------------------------------------------------
    public String toString()
    {
        String result = "";

        MagazineNode current = list;

        while (current != null)
        {
            result += current.magazine + "\n";
            current = current.next;
        }

        return result;
    }

    public void Sort(){
        Magazine temp;
        if (list.magazine.compareTo(list.next.magazine) > 0){
            temp = list.next.magazine;
            list.next.magazine = list.magazine;
            list.magazine = temp;
        }
        MagazineNode temp1 = list;
        MagazineNode temp2;

        while (temp1.next != null){
            temp2 = temp1.next;
            while (temp2.next != null){
                if (temp1.next.magazine.compareTo(temp2.next.magazine) > 0){
                    temp = temp2.next.magazine;
                    temp2.next.magazine = temp1.next.magazine;
                    temp1.next.magazine = temp;
                }
                temp2 = temp2.next;
            }
            temp1 = temp1.next;
        }

    }

    //*****************************************************************
    //  An inner class that represents a node in the magazine list.
    //  The public variables are accessed by the MagazineList class.
    //*****************************************************************
    private class MagazineNode
    {
        public Magazine magazine;
        public MagazineNode next;

        //--------------------------------------------------------------
        //  Sets up the node
        //--------------------------------------------------------------
        public MagazineNode(Magazine mag)
        {
            magazine = mag;
            next = null;

        }
    }
}
